﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{
    public partial class AdminAddNewWorker : Form
    {

        public AdminAddNewWorker()
        {
            SqlConnection dbConnection2 = SqlConnectionFactory.getConnection();
            InitializeComponent();
        }

        Worker editedWorker;
        bool editMode;
        public event Action Closed;

        public AdminAddNewWorker(Worker worker, bool edit)
        {
            editedWorker = worker;
            editMode = edit;
            InitializeComponent();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            bool fieldsFilledOut = true;

            try
            {
                long test = Convert.ToInt64(peselBox.Text.Trim().ToString());
            }
            catch (FormatException ex)
            {
                fieldsFilledOut = false;
                MessageBox.Show("PESEL musi byc liczba!");
            }

            try
            {
                string spoko = occupationBox.SelectedItem.ToString();
            }
            catch (NullReferenceException ex)
            {
                fieldsFilledOut = false;
                MessageBox.Show("Wybierz zawód pracownika");
            }

            if (peselBox.Text.Trim().ToString().Length != 11)
            {
                fieldsFilledOut = false;
                MessageBox.Show("PESEL musi składać się z 11 cyfr");
            }

            if (nameBox.Text.Trim() == String.Empty)
            {
                nameError.Text = "Podaj imię";
                nameError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (surnameBox.Text.Trim() == String.Empty)
            {
                surnameError.Text = "Podaj nazwisko";
                surnameError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (peselBox.Text.Trim() == String.Empty)
            {
                peselError.Text = "Podaj PESEL";
                peselError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }
            if (editMode == false)
            {
                if ((fieldsFilledOut) && (!Worker.checkIfRecordExists(peselBox.Text.Trim().ToString(), "Worker")))
                {
                    
                    Worker worker = Worker.getByPesel(long.Parse(peselBox.Text.Trim().ToString()));
                    if (worker == null)
                    {
                        worker = new Worker();
                        worker.active = true;
                    }
                    
                    worker.name = nameBox.Text.Trim().ToString();
                    worker.surname = surnameBox.Text.Trim().ToString();
                    worker.birthdate = dateBox.Value;
                    worker.pesel = long.Parse(peselBox.Text.Trim().ToString());

                    string spoko = occupationBox.SelectedItem.ToString();
                    int occupationID = 1;
                    switch (spoko)
                    {
                        case "admin":
                            occupationID = 4;
                            break;
                        case "dyspozytor":
                            occupationID = 2;
                            break;
                        case "kasjer":
                            occupationID = 3;
                            break;
                        case "kierowca":
                            occupationID = 1;
                            break;
                        case "statystyk":
                            occupationID = 5;
                            break;
                    }
                    worker.occupation = Occupation.getById(occupationID);
                    worker.active = true;
                    worker.save();

                    if (licensesBox.CheckedItems.Contains("B") == true)
                    {
                        WorkerLicence workerLicence = new WorkerLicence();
                        workerLicence.driver_id = Worker.getIdByPesel(worker.pesel);
                        workerLicence.licence_id = 1;
                        workerLicence.save();
                    }

                    if (licensesBox.CheckedItems.Contains("B+E") == true)
                    {
                        WorkerLicence workerLicence = new WorkerLicence();
                        workerLicence.driver_id = Worker.getIdByPesel(worker.pesel);
                        workerLicence.licence_id = 2;
                        workerLicence.save();
                    }

                    if (licensesBox.CheckedItems.Contains("C") == true)
                    {
                        WorkerLicence workerLicence = new WorkerLicence();
                        workerLicence.driver_id = Worker.getIdByPesel(worker.pesel);
                        workerLicence.licence_id = 3;
                        workerLicence.save();
                    }

                    if (licensesBox.CheckedItems.Contains("C+E") == true)
                    {
                        WorkerLicence workerLicence = new WorkerLicence();
                        workerLicence.driver_id = Worker.getIdByPesel(worker.pesel);
                        workerLicence.licence_id = 4;
                        workerLicence.save();
                    }

                    if (licensesBox.CheckedItems.Contains("C1") == true)
                    {
                        WorkerLicence workerLicence = new WorkerLicence();
                        workerLicence.driver_id = Worker.getIdByPesel(worker.pesel);
                        workerLicence.licence_id = 5;
                        workerLicence.save();
                    }

                    if (licensesBox.CheckedItems.Contains("D") == true)
                    {
                        WorkerLicence workerLicence = new WorkerLicence();
                        workerLicence.driver_id = Worker.getIdByPesel(worker.pesel);
                        workerLicence.licence_id = 6;
                        workerLicence.save();
                    }

                    if (licensesBox.CheckedItems.Contains("D+E") == true)
                    {
                        WorkerLicence workerLicence = new WorkerLicence();
                        workerLicence.driver_id = Worker.getIdByPesel(worker.pesel);
                        workerLicence.licence_id = 7;
                        workerLicence.save();
                    }

                    if (licensesBox.CheckedItems.Contains("D1") == true)
                    {
                        WorkerLicence workerLicence = new WorkerLicence();
                        workerLicence.driver_id = Worker.getIdByPesel(worker.pesel);
                        workerLicence.licence_id = 8;
                        workerLicence.save();
                    }

                    MessageBox.Show("Dodano pracownika");
                    this.Dispose();

                    if (Closed != null)
                    {
                        Closed();
                    }

                }
            }

            if (editMode == true)
                if (fieldsFilledOut) 
                {
                    {
                        editedWorker.name = nameBox.Text.Trim().ToString();
                        editedWorker.surname = surnameBox.Text.Trim().ToString();
                        editedWorker.birthdate = dateBox.Value;
                        editedWorker.pesel = long.Parse(peselBox.Text.Trim().ToString());
                        string spoko = occupationBox.SelectedItem.ToString();
                        int occupationID = 1;
                        switch (spoko)
                        {
                            case "admin":
                                occupationID = 4;
                                break;
                            case "dyspozytor":
                                occupationID = 2;
                                break;
                            case "kasjer":
                                occupationID = 3;
                                break;
                            case "kierowca":
                                occupationID = 1;
                                break;
                            case "statystyk":
                                occupationID = 5;
                                break;
                        }
                        editedWorker.occupation = Occupation.getById(occupationID);
                        editedWorker.active = true;
                        editedWorker.save();

                        if (licensesBox.CheckedItems.Contains("B") == true)
                        {
                            WorkerLicence workerLicence = new WorkerLicence();
                            workerLicence.driver_id = Worker.getIdByPesel(editedWorker.pesel);
                            workerLicence.licence_id = 1;
                            workerLicence.save();
                        }

                        if (licensesBox.CheckedItems.Contains("B+E") == true)
                        {
                            WorkerLicence workerLicence = new WorkerLicence();
                            workerLicence.driver_id = Worker.getIdByPesel(editedWorker.pesel);
                            workerLicence.licence_id = 2;
                            workerLicence.save();
                        }

                        if (licensesBox.CheckedItems.Contains("C") == true)
                        {
                            WorkerLicence workerLicence = new WorkerLicence();
                            workerLicence.driver_id = Worker.getIdByPesel(editedWorker.pesel);
                            workerLicence.licence_id = 3;
                            workerLicence.save();
                        }

                        if (licensesBox.CheckedItems.Contains("C+E") == true)
                        {
                            WorkerLicence workerLicence = new WorkerLicence();
                            workerLicence.driver_id = Worker.getIdByPesel(editedWorker.pesel);
                            workerLicence.licence_id = 4;
                            workerLicence.save();
                        }

                        if (licensesBox.CheckedItems.Contains("C1") == true)
                        {
                            WorkerLicence workerLicence = new WorkerLicence();
                            workerLicence.driver_id = Worker.getIdByPesel(editedWorker.pesel);
                            workerLicence.licence_id = 5;
                            workerLicence.save();
                        }

                        if (licensesBox.CheckedItems.Contains("D") == true)
                        {
                            WorkerLicence workerLicence = new WorkerLicence();
                            workerLicence.driver_id = Worker.getIdByPesel(editedWorker.pesel);
                            workerLicence.licence_id = 6;
                            workerLicence.save();
                        }

                        if (licensesBox.CheckedItems.Contains("D+E") == true)
                        {
                            WorkerLicence workerLicence = new WorkerLicence();
                            workerLicence.driver_id = Worker.getIdByPesel(editedWorker.pesel);
                            workerLicence.licence_id = 7;
                            workerLicence.save();
                        }

                        if (licensesBox.CheckedItems.Contains("D1") == true)
                        {
                            WorkerLicence workerLicence = new WorkerLicence();
                            workerLicence.driver_id = Worker.getIdByPesel(editedWorker.pesel);
                            workerLicence.licence_id = 8;
                            workerLicence.save();
                        }
                        MessageBox.Show("Zmieniono pracownika");

                        this.Dispose();

                        if (Closed != null)
                        {
                            Closed();
                        }
                    }
                }
        }

        private void AdminAddNewWorker_Load(object sender, EventArgs e)
        {
            if (editMode == true)
            {
                nameBox.Text = editedWorker.name.ToString();
                surnameBox.Text = editedWorker.surname.ToString();
                dateBox.Value = editedWorker.birthdate;
                peselBox.Text = editedWorker.pesel.ToString();
                int occupation = editedWorker.occupation.id;
                switch (occupation)
                {
                    case 4:
                        occupationBox.SelectedIndex = 0;
                        break;
                    case 1:
                        occupationBox.SelectedIndex = 1;
                        break;
                    case 2:
                        occupationBox.SelectedIndex = 2;
                        break;
                    case 3:
                        occupationBox.SelectedIndex = 3;
                        break;
                    case 5:
                        occupationBox.SelectedIndex = 4;
                        break;
                }
                Worker.checkLicences(editedWorker, licensesBox);
            }
        }

        private void AdminAddNewWorker_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            HelpUtils.showHelp(this, 23);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F1)
            {
                if (editMode == false)
                    HelpUtils.showHelp(this, 23);
                else
                    HelpUtils.showHelp(this, 24);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
